﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>3.静态函数Configure的使用</title>
	<script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
	<script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
	<link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
	<script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
	<h3>3.静态函数Configure的使用</h3>
	<p>
		<b>1.定义接口和类，为类添加静态</b><br />
		<pre class="brush: csharp">
			//声明IAnimal接口
			public interface IAnimal
			{
				void Talk();
			}
			//声明Dog类
            public class Dog : IAnimal
            {
				//定义静态变量
				public static string Sex;
                public Talk()
				{
					Console.WriteLine("我是一只"+Sex+"狗！");
				}
				//定义初始化配置，该函数必须为该格式，其中参数可以任意定义
				//但函数名，返回类型等是不能修改的。若无初始化操作，也可不定义。
				public static void Configure(Sex)
                {
                    Dog.Sex=Sex;
                }
            }
        </pre>
	</p>
	<p>
		<b>2.使用Configure()函数执行自定义配置</b><br />
		<pre class="brush: csharp">
		public static NFinal.DependencyInjection.ServiceCollection serviceCollection=new NFinal.DependencyInjection.ServiceCollection();
        public static void Main(string[] args)
        {
			//注入Dog类，并执行配置函数
			serviceCollection.Set&lt;IAnimal>(typeof(Dog)).Configure("公");
			//获取Dog对象
			IAnimal animal=serviceCollection.Get&lt;IAnimal>();
			//执行Talk函数并输出:"我是一只公狗！"
			animal.Talk();
		}
		</pre>
	</p>
</body>
</html>
